package com.teacold.customdemo.chapter_one;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class RectPointView extends View {
    private Paint mPaint;
    private Rect mRect;
    private float mX;//点击的时候的X坐标
    private float mY;//点击的时候的Y坐标
    private Rect mRect2;

    public RectPointView(Context context) {
        super(context);
        init();
    }

    public RectPointView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public RectPointView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
      mPaint =new Paint();
      mPaint.setStyle(Paint.Style.FILL);
      mRect=new Rect(100,10,600,401);
      mRect2=new Rect(110,400,500,500);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(mRect.contains((int) mX,(int) mY)){//点击到了矩形  原谅色走起
            mPaint.setColor(Color.BLUE);
        }else {//没有点击到矩形  红色走起
            if(Rect.intersects(mRect2,mRect)){
                mPaint.setColor(Color.WHITE);
            }else {
                mPaint.setColor(Color.RED);
            }
        }
        canvas.drawRect(mRect,mPaint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mX=event.getX();
        mY=event.getY();
        if(event.getAction()==MotionEvent.ACTION_DOWN){
            invalidate();
            return true;
        }else if(event.getAction()==MotionEvent.ACTION_UP){
            mX =-1;
            mY=-1;
        }
        invalidate();
        return super.onTouchEvent(event);
    }
}
